package com.system.enterprisemanagementsystem.controller;


import com.github.pagehelper.Page;
import com.system.enterprisemanagementsystem.anno.LogOperation;
import com.system.enterprisemanagementsystem.exception.BusinessException;
import com.system.enterprisemanagementsystem.pojo.*;
import com.system.enterprisemanagementsystem.service.StudentService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

@Slf4j
@RequestMapping("/students")
@RestController
public class StudentController {

    @Autowired
    private StudentService studentService;

    @GetMapping
    public Result pageStudents(StudentQueryParam studentQueryParam){
        log.info("学员列表分页查询");
        PageResult pageResult = studentService.page(studentQueryParam);
        return Result.success(pageResult);
    }

    @GetMapping("/{id}")
    public Result getStudentById(@PathVariable Integer id) {

        log.info("根据id查询学生");
        return Result.success(studentService.getStudentById(id));
    }

    @PostMapping
    @LogOperation
    public Result addStudent(@RequestBody Student student) {
        log.info("添加学员: {}", student);
        studentService.addStudent(student);
        return Result.success();
    }


    @PutMapping
    @LogOperation
    public Result updateStudent(@RequestBody Student student) {
        log.info("更新学生信息 {}",student);
        studentService.updateStudent(student);
        return Result.success();
    }


    /**
     * 批量删除学员（直接接收数组）
     * DELETE /students/1,2,3
     */
    @DeleteMapping("/{ids}")
    @LogOperation
    public Result deleteStudents(@PathVariable("ids") Integer[] ids) {
        log.info("批量删除学员, ids: {}", Arrays.toString(ids));

        try {
            if (ids == null || ids.length == 0) {
                return Result.error("请选择要删除的学员");
            }

            List<Integer> idList = Arrays.asList(ids);
            studentService.deleteStudentsByIds(idList);
            return Result.success("删除成功");

        } catch (BusinessException e) {
            return Result.error(e.getMessage());
        } catch (Exception e) {
            log.error("批量删除学员异常, ids: {}", Arrays.toString(ids), e);
            return Result.error("删除失败，请稍后重试");
        }
    }

    @PutMapping("/violation/{id}/{score}")
    public Result violationHandling(@PathVariable Integer id, @PathVariable Integer score) {
        log.info("学员违纪处理, 学员ID: {}, 扣分: {}", id, score);

        // 参数校验
        if (id == null || id <= 0) {
            return Result.error("学员ID参数错误");
        }
        if (score == null || score <= 0) {
            return Result.error("扣分分数必须大于0");
        }

        try {
            // 调用Service进行违纪处理
            studentService.handleViolation(id, score);
            return Result.success("违纪处理成功");

        } catch (BusinessException e) {
            return Result.error(e.getMessage());
        } catch (Exception e) {
            log.error("违纪处理异常, id: {}, score: {}", id, score, e);
            return Result.error("处理失败，请稍后重试");
        }

    }



}
